package com.yanxx.codegenerator.reset.impl;

import com.yanxx.codegenerator.reset.ResetStrategy;

import java.time.LocalDateTime;
import java.time.temporal.WeekFields;
import java.util.Locale;

/**
 * 按周重置策略
 * 每周重置一次流水号
 */
public class WeeklyResetStrategy implements ResetStrategy {
    
    private final WeekFields weekFields = WeekFields.of(Locale.getDefault());
    
    @Override
    public boolean shouldReset(LocalDateTime lastResetTime, LocalDateTime currentTime) {
        if (lastResetTime == null) {
            return true;
        }
        
        // 如果年份不同或周数不同，则需要重置
        int lastWeek = lastResetTime.get(weekFields.weekOfWeekBasedYear());
        int lastYear = lastResetTime.get(weekFields.weekBasedYear());
        int currentWeek = currentTime.get(weekFields.weekOfWeekBasedYear());
        int currentYear = currentTime.get(weekFields.weekBasedYear());
        
        return lastYear != currentYear || lastWeek != currentWeek;
    }
    
    @Override
    public String getType() {
        return "WEEKLY";
    }
}